package com.inossem.warehousestorage_inossem.adapter.outStorage;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStorageAllocatingListActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStorageAllocatingOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.extra.OutStorageExtra;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutAllocatingStorageListBean;

import java.util.List;

/**
 * 作者：wsy on 2017/12/4 16:47
 * <p>
 * 邮箱：445979770@qq.com
 */

public class AllocatingOutStorageListAdapter extends BaseAdapter {

    private Context mContext;
    private List<OutAllocatingStorageListBean.BodyBean> mAllocatingOutStorageList;
    private LayoutInflater inflater;
    private OutStorageAllocatingListActivity.OutStorageAllocatingListener mOutStorageAllocatingListener;
    private String mPickingNumber;

    public AllocatingOutStorageListAdapter(Context context, List<OutAllocatingStorageListBean.BodyBean> pickingOutStorageList, String pickingNumber, OutStorageAllocatingListActivity.OutStorageAllocatingListener outStoragePickingListener) {
        mContext = context;
        mAllocatingOutStorageList = pickingOutStorageList;
        mOutStorageAllocatingListener = outStoragePickingListener;
        inflater = LayoutInflater.from(context);
        mPickingNumber = pickingNumber;
    }

    @Override
    public int getCount() {
        return mAllocatingOutStorageList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mAllocatingOutStorageList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_out_storage_allocating_list_first_item, null);
            EditText inStorageNumber = (EditText) convertView.findViewById(R.id.inStorageNumber);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            inStorageNumber.setText(mPickingNumber == null ? "" : mPickingNumber);
            inStorageNumber.setHint(mContext.getResources().getString(R.string.activity_in_storage_allocating_list_please_enter_the_dial_number_or_the_distribution_number));
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    mOutStorageAllocatingListener.getNoDoubleClickListener(inStorageNumber.getText().toString());
                }
            });
        } else {
            OutAllocatingStorageListBean.BodyBean allocatingOutInstorageItem = mAllocatingOutStorageList.get(position - 1);
            AllocatingOutStorageListAdapter.ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_out_storage_allocating_list, null);
                viewHolder = new AllocatingOutStorageListAdapter.ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (AllocatingOutStorageListAdapter.ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, allocatingOutInstorageItem);
        }
        return convertView;
    }

    private void initView(AllocatingOutStorageListAdapter.ViewHolder viewHolder, View view) {
        viewHolder.allocatingNumber = (TextView) view.findViewById(R.id.allocatingNumber);
        viewHolder.allocatingNumberValue = (TextView) view.findViewById(R.id.allocatingNumberValue);
        viewHolder.deliveryNumber = (TextView) view.findViewById(R.id.deliveryNumber);
        viewHolder.deliveryNumberValue = (TextView) view.findViewById(R.id.deliveryNumberValue);
        viewHolder.sendFatory = (TextView) view.findViewById(R.id.sendFatory);
        viewHolder.sendLocation = (TextView) view.findViewById(R.id.sendLocation);
    }

    private void initData(Context context, AllocatingOutStorageListAdapter.ViewHolder viewHolder, View view, OutAllocatingStorageListBean.BodyBean allocatingOutStorageItem) {
        viewHolder.allocatingNumber.setText(allocatingOutStorageItem.getAllocate_code());
        viewHolder.allocatingNumberValue.setText(allocatingOutStorageItem.getAllocate_id());
        viewHolder.deliveryNumber.setText(allocatingOutStorageItem.getAllocate_delivery_code());
        viewHolder.deliveryNumberValue.setText(allocatingOutStorageItem.getAllocate_delivery_id());
        viewHolder.sendFatory.setText(allocatingOutStorageItem.getFty_name_output());
        viewHolder.sendLocation.setText(allocatingOutStorageItem.getLocation_name_output());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, OutStorageAllocatingOverviewActivity.class);
                intent.putExtra(OutStorageExtra.TRANSFER_NUMBER, viewHolder.allocatingNumberValue.getText().toString());
                intent.putExtra(OutStorageExtra.DELIVERY_NUMBER, viewHolder.deliveryNumberValue.getText().toString());
                context.startActivity(intent);
            }
        });
    }

    private class ViewHolder {
        private TextView allocatingNumber, allocatingNumberValue, deliveryNumber, deliveryNumberValue, sendFatory, sendLocation;
    }
}
